package com.ship.boats.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.ship.boats.business.UploadPortFileBusiness;
import com.ship.boats.domain.SpBoatsReceivingStation;
import com.ship.boats.domain.SpBoatsWeather;
import com.ship.boats.domain.dto.QueryReceivStationRq;
import com.ship.boats.domain.vo.QueryBoatsReceivingStationInfo;
import com.ship.boats.domain.vo.QueryBoatsWeatherInfo;
import com.ship.boats.domain.vo.QueryHistoryForwardingChargesInfo;
import com.ship.boats.domain.vo.QueryHistoryForwardingInfo;
import com.ship.boats.service.BoatsReceivingStationService;
import com.ship.boats.service.BoatsWeatherService;
import com.ship.boats.service.SpShipAgencyFeeHistoryService;
import com.ship.boats.util.ExportDownloadUtil;
import com.ship.common.core.exception.ServiceException;
import com.ship.common.core.utils.FileUtils;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>BoatsReceivingStationController</b>
 *
 * @description: BoatsReceivingStationController <br>
 * @date: 2024/4/16 16:59 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
@Api(tags = "接收站-港口模块")
@RestController
public class BoatsReceivingStationController {
    private static final Logger logger = LoggerFactory.getLogger(BoatsReceivingStationController.class);

    @Autowired
    private BoatsReceivingStationService boatsReceivingStationService;

    @Autowired
    private BoatsWeatherService boatsWeatherService;

    @Autowired
    private ExportDownloadUtil exportDownloadUtil;

    @Autowired
    private SpShipAgencyFeeHistoryService spShipAgencyFeeHistoryService;

    @Autowired
    private UploadPortFileBusiness uploadFileBusiness;

    @ApiOperation("根据类型查询分页条件")
    @GetMapping("/query/receivintStation/byType")
    public JsonResult queryBoatsReceivintStationByType(@RequestParam String type){
        List<String> list = boatsReceivingStationService.queryBoatsReceivintStationByType(type);
        return JsonResult.success(list);
    }

    @ApiOperation("上传接收站信息")
    @PostMapping("/upload/receivintStation")
    public JsonResult uploadReceivintStation(@RequestParam("file") MultipartFile file) throws Exception {
        FileUtils.checkFileName(file);
        List<SpBoatsReceivingStation> receivingStationList = uploadFileBusiness.uploadList(file);
        boolean status = boatsReceivingStationService.createReceivintStation(receivingStationList);
        return JsonResult.success(status);
    }

    @ApiOperation("分页查询接收站列表")
    @PostMapping("/query/receivintStation/list")
    public JsonResult queryBoatsReceivingStationList(@RequestBody QueryReceivStationRq rq){
        logger.info("分页查询接收站列表入参:{}"+ JSONUtil.parse(rq));
        PageInfo<SpBoatsReceivingStation> pageInfo = boatsReceivingStationService.queryBoatsReceivingStationList(rq);
        logger.info("分页查询接收站列表返回结果:{}"+JSONUtil.parse(pageInfo));
        return JsonResult.success(pageInfo);
    }

    @ApiOperation("根据ID查询接收站详情")
    @GetMapping("/query/receivintStation/byId")
    public JsonResult queryBoatsReceivintStationById(@RequestParam String id){
        QueryBoatsReceivingStationInfo info = boatsReceivingStationService.queryBoatsReceivintStationById(id);
        logger.info("根据ID查询接收站详情返回结果:{}"+JSONUtil.parse(info));
        return JsonResult.success(info);
    }

    @ApiOperation("根据ID分页查询天气列表")
    @GetMapping("/query/boatsWeather/list")
    public JsonResult queryBoatsWeatherList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                            @RequestParam String id){
       PageInfo<QueryBoatsWeatherInfo> pageInfo = boatsWeatherService.queryBoatsWeatherList(pageNum,pageSize,id);
       logger.info("根据ID份额查询天气列表返回结果:{}"+JSONUtil.parse(pageInfo));
       return JsonResult.success(pageInfo);
    }

    @ApiOperation("导出天气列表")
    @GetMapping("/export/boatsWeather")
    public JsonResult exportBoatsWeather(@RequestParam String id){
        List<QueryBoatsWeatherInfo> boatsWeatherList = boatsWeatherService.exportBoatsWeather(id);
        String name = boatsReceivingStationService.queryReceivingStationName(id);
        Field[] fields = QueryBoatsWeatherInfo.class.getDeclaredFields();
        List<String> tableList = new ArrayList<>();
        tableList.add("createTime");
        tableList.add("temp");
        tableList.add("visibility");
        tableList.add("windDirection");
        tableList.add("windSpeed");
        tableList.add("waterDirection");
        tableList.add("waterSpeed");
        tableList.add("seaSwellDirection");
        tableList.add("seaSwellHeight");
        tableList.add("seaWaveDirection");
        tableList.add("seaWaveHeight");
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("日期");
        tableNameList.add("温度℃");
        tableNameList.add("能见度km");
        tableNameList.add("风向°");
        tableNameList.add("风速m/s");
        tableNameList.add("流向°");
        tableNameList.add("流速m/s");
        tableNameList.add("涌浪向°");
        tableNameList.add("涌浪高m");
        tableNameList.add("海浪向°");
        tableNameList.add("海浪高m");
        String url = exportDownloadUtil.exportUtil(boatsWeatherList,fields,
                tableList,tableNameList,
                name+"天气列表");
        return JsonResult.success(url);
    }

    @ApiOperation("导出接收站列表")
    @PostMapping("/export/receivintStation")
    public JsonResult exportReceivingStation(@RequestBody QueryReceivStationRq rq){
        List<SpBoatsReceivingStation> receivingStationList = boatsReceivingStationService.exportReceivingStation(rq);
        Field[] fields = SpBoatsReceivingStation.class.getDeclaredFields();
        String url = exportDownloadUtil.pageExport(receivingStationList,fields,
                exportDownloadUtil.listStrByModuleAndUser("sp_boats_receiving_station",
                        SecurityUtils.getUserId()),
                "接收站信息");
        return JsonResult.success(url);
    }

    @ApiOperation("根据接收站ID分页查询历史船代费列表")
    @GetMapping("/query/historyForwardingCharges")
    public JsonResult queryHistoryForwardingCharges(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                    @RequestParam String id){
        QueryHistoryForwardingInfo info = spShipAgencyFeeHistoryService.queryHistoryForwardingCharges(pageNum,pageSize,id);
        return JsonResult.success(info);
    }

    @ApiOperation("导出历史船代费")
    @GetMapping("/export/historyForwardingCharges")
    public JsonResult exportHistoryForwardingCharges(@RequestParam String id){
        List<QueryHistoryForwardingChargesInfo> list = spShipAgencyFeeHistoryService.exportHistoryForwardingCharges(id);
        String name = boatsReceivingStationService.queryReceivingStationName(id);
        Field[] fields = QueryHistoryForwardingChargesInfo.class.getDeclaredFields();
        List<String> tableList = new ArrayList<>();
        tableList.add("voyageNo");
        tableList.add("name");
        tableList.add("date");
        tableList.add("shipAgencyFee");
        List<String> tableNameList = new ArrayList<>();
        tableNameList.add("航次好");
        tableNameList.add("接收站名称");
        tableNameList.add("时间");
        tableNameList.add("船代费");
        String url = exportDownloadUtil.exportUtil(list,fields,
                tableList,tableNameList,
                name+"历史船代费");
        return JsonResult.success(url);
    }
}
